﻿using System.Collections;
using UnityEngine;
namespace Character.Jarily
{
#if UNITY_EDITOR
    using UnityEditor;
    using UnityEditorInternal;
    [CustomEditor(typeof(AiBehaviourPool))]
    public class AiBehaviourPoolEditor : Editor
    {
        ReorderableList m_reorderableList_Remote;
        ReorderableList m_reorderableList_Short;
        private void OnEnable()
        {
            var m_RemoteAttack = serializedObject.FindProperty("remoteAttack");
            var m_ShortAttack = serializedObject.FindProperty("shortAttack");
            m_reorderableList_Remote = new ReorderableList(serializedObject, m_RemoteAttack);
            m_reorderableList_Short = new ReorderableList(serializedObject, m_ShortAttack);
            m_reorderableList_Remote.elementHeight = EditorGUIUtility.singleLineHeight * 2 + 6;
            m_reorderableList_Short.elementHeight = EditorGUIUtility.singleLineHeight * 2 + 6;
            m_reorderableList_Remote.drawElementCallback = (rect, index, isActive, isFocused) =>
            {
                var element = m_RemoteAttack.GetArrayElementAtIndex(index);
                rect.height -= 2;
                rect.y += 1;
                EditorGUI.PropertyField(rect, element);
            };
            m_reorderableList_Short.drawElementCallback = (rect, index, isActive, isFocused) =>
            {
                var element = m_ShortAttack.GetArrayElementAtIndex(index);
                rect.height -= 2;
                rect.y += 1;
                EditorGUI.PropertyField(rect, element);
            };
            m_reorderableList_Remote.onAddCallback = (list) =>
            {
                m_RemoteAttack.arraySize++;
                list.index = m_RemoteAttack.arraySize - 1;
            };
            m_reorderableList_Short.onAddCallback = (list) =>
            {
                m_ShortAttack.arraySize++;
                list.index = m_ShortAttack.arraySize - 1;
            };
            m_reorderableList_Remote.drawHeaderCallback = (rect) =>
            EditorGUI.LabelField(rect, m_RemoteAttack.displayName);
            m_reorderableList_Short.drawHeaderCallback = (rect) =>
            EditorGUI.LabelField(rect, m_ShortAttack.displayName);
        }
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            m_reorderableList_Remote.DoLayoutList();
            m_reorderableList_Short.DoLayoutList();
            serializedObject.ApplyModifiedProperties();
        }
    }
    [CustomPropertyDrawer(typeof(ShortAttack))]
    public class ShortAttackDrawer : PropertyDrawer
    {
        ShortAttack shortAttack;
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            using (new EditorGUI.PropertyScope(position, label, property))
            {
                EditorGUIUtility.labelWidth = 40;
                position.height = EditorGUIUtility.singleLineHeight;
                var halfWidth = position.width * 0.5f;
                var colliderRect = new Rect(position)
                {
                    width = position.width / 4,
                };
                var animAttributeRect = new Rect(position)
                {
                    width = colliderRect.width * 2,
                    x = position.x + colliderRect.width + 5,
                };
                var batterRect = new Rect(animAttributeRect)
                {
                    width = colliderRect.width,
                    x = animAttributeRect.x + colliderRect.width * 2 + 5,
                };
                var forceRect = new Rect(position)
                {
                    width = position.width / 3,
                    y = position.y + EditorGUIUtility.singleLineHeight + 2,
                };
                var attackValueRect = new Rect(forceRect)
                {
                    x = forceRect.x + position.width / 3
                    ,
                    width = 2 * position.width / 3
                };
                var collider = property.FindPropertyRelative("collider");
                var attackValue = property.FindPropertyRelative("attackValue");
                var force = property.FindPropertyRelative("force");
                var batter = property.FindPropertyRelative("batter");
                var animAttribute = property.FindPropertyRelative("animAttribute");
                collider.objectReferenceValue = EditorGUI.ObjectField(colliderRect, collider.objectReferenceValue, typeof(Collider2D), false);
                EditorGUIUtility.labelWidth = 90;
                animAttribute.stringValue = EditorGUI.TextField(animAttributeRect, animAttribute.displayName, animAttribute.stringValue);
                EditorGUIUtility.labelWidth = 40;
                force.vector2Value = EditorGUI.Vector2Field(forceRect, force.displayName, force.vector2Value);
                EditorGUIUtility.labelWidth = 90;
                attackValue.intValue = EditorGUI.IntSlider(attackValueRect, attackValue.displayName, attackValue.intValue, 0, 10);
                EditorGUIUtility.labelWidth = 45;
                batter.boolValue = EditorGUI.Toggle(batterRect, batter.displayName, batter.boolValue);
            }
        }
        [CustomPropertyDrawer(typeof(RemoteAttack))]
        public class RemoteAttackDrawer : PropertyDrawer
        {
            RemoteAttack remoteAttack;
            public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
            {
                using (new EditorGUI.PropertyScope(position, label, property))
                {
                    position.height = EditorGUIUtility.singleLineHeight;
                    var halfWidth = position.width * 0.5f;
                    var animAttributeRect = new Rect(position)
                    {
                        width = 4 * position.width / 5
                        ,
                        height = position.height - 2
                    };
                    var batterRect = new Rect(animAttributeRect)
                    {
                        x = animAttributeRect.x + animAttributeRect.width + 5,
                        width = position.width / 5
                    };
                    var ProjectileRect = new Rect(position)
                    {
                        y = position.y + EditorGUIUtility.singleLineHeight + 2
                    };
                    var animAttribute = property.FindPropertyRelative("animAttribute");
                    var batter = property.FindPropertyRelative("batter");
                    var Projectile = property.FindPropertyRelative("physicalProjectile");
                    EditorGUIUtility.labelWidth = 90;
                    animAttribute.stringValue = EditorGUI.TextField(animAttributeRect, animAttribute.displayName, animAttribute.stringValue);
                    EditorGUIUtility.labelWidth = 45;
                    batter.boolValue = EditorGUI.Toggle(batterRect, batter.displayName, batter.boolValue);
                    EditorGUIUtility.labelWidth = 110;
                    Projectile.objectReferenceValue = EditorGUI.ObjectField(ProjectileRect, Projectile.displayName, Projectile.objectReferenceValue, typeof(GameObject), false);
                }
            }
        }
    }
#endif
}